<template>
  <el-dialog
    :visible.sync="visible"
    :close-on-click-modal="false"
    title="账单领取"
    @close="getConClose"
    width="591px"
    class="get_contract_dialog"
  >
    <el-form
      ref="form"
      :model="contractForm"
      :rules="contractRules"
      label-width="80px"
      label-position="top"
      class="get_form"
      :inline="true"
    >
      <div class="flex_box">
        <el-form-item label="关联学员" prop="customerId" style="width: 259px">
          <crm-relative-cell
            :value="contractForm.customerId"
            relative-type="customer"
            @value-change="otherChange"
          />
        </el-form-item>
        <el-form-item label="学员手机号" prop="phone" style="width: 259px">
          <el-input
            v-model="contractForm.phone"
            placeholder="请输入手机号"
            clearable
          />
        </el-form-item>
      </div>
      <!-- <el-form-item
        label="助教协作人"
        prop="assistantUserId"
        style="width: 259px"
      >
        <UserSelect
          v-model="contractForm.assistantUserId"
          @input="user2Change"
          style="width: 100%"
        ></UserSelect>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="getConClose">取 消</el-button>
      <el-button type="primary" @click="getConSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { chinaMobileRegex } from "@/utils";
import UserSelect from "@/views/admin/components/UserSelect";
import { CrmRelativeCell } from "@/components/CreateCom";
import { changeOrderPoolUser } from "@/api/crm/contractPool";
import { mapGetters } from "vuex";
export default {
  /**
   * 账单-领取
   */
  name: "GetsContract",
  components: {
    UserSelect,
    CrmRelativeCell,
  },
  data() {
    return {
      visible: false,
      contractForm: {
        phone: "", //手机号
        // assistantUserId: "", //助教协作人
        customerId: [], //绑定的学员Id
      },
      contractRules: {
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: chinaMobileRegex,
            message: "目前只支持中国大陆的手机号码",
            trigger: "blur",
          },
        ],
        // assistantUserId: [
        //   { required: true, message: "助教协作人不能为空", trigger: "change" },
        // ],
        customerId: [
          { required: true, message: "学员不能为空", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  watch: {
    getConDialog: {
      handler(val) {
        if (val) {
          this.visible = true;
        } else {
          this.visible = false;
        }
      },
      immediate: true,
    },
  },
  props: ["getConDialog", "rowDatas"],
  methods: {
    /**
     * 弹框-关闭
     */
    getConClose() {
      this.contractForm = {
        phone: "",
        // assistantUserId: "",
        customerId: [],
      };
      this.$refs.form.resetFields();
      this.visible = false;
    },
    /**
     * 弹框-确定
     */
    getConSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          changeOrderPoolUser({
            phone: this.contractForm.phone,
            customerId: this.contractForm.customerId[0].customerId,
            // assistantUserId: this.contractForm.assistantUserId,
            ids: [this.rowDatas.contractId],
            ownerUserId: this.userInfo.userId,
          }).then((res) => {
            if (res.code == 0) {
              this.$emit("submit");
              this.$message.success(res.msg);
              this.visible = false;
            } else {
              this.$message.error(res.msg, 2000);
            }
          });
        }
      });
    },
    /**
     *  学员-change
     */
    otherChange(data, field) {
      if (data.value && data.value.length > 0) {
        this.contractForm.customerId = data.value;
      } else {
        this.contractForm.customerId = [];
      }
      this.$refs.form.validateField("customerId");
    },
    /**
     *  助教协作人-change
     */
    // user2Change(data) {
    //   this.contractForm["assistantUserId"] = data;
    // },
  },
};
</script>
<style scoped lang="scss">
.get_contract_dialog {
  /deep/ .el-dialog__header {
    padding-top: 25px !important;
  }
  /deep/ .el-dialog__body {
    padding: 14px 23px !important;
  }
  .get_form {
    /deep/ .el-form-item__label {
      line-height: 6px !important;
    }
  }
  /deep/ .el-form--inline .el-form-item {
    margin-right: 0 !important;
  }
}
.flex_box {
  display: flex;
  justify-content: space-between;
}
</style>